package org.inftel.tms.simulator.view;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.http.HttpResponse;
import org.inftel.tms.simulator.controller.Fachada;

/**
 * 
 * @author migueqm
 */
public class Main extends javax.swing.JFrame {

	private static final long serialVersionUID = 1L;

	private Fachada fachada;

	/**
	 * Creates new form Main
	 */
	public Main() {
		fachada = new Fachada();
		initComponents();
		btnDeviceAlarm.setVisible(false);
		btnUserAlarm.setVisible(false);
		btnTechnicalAlarm.setVisible(false);
		comboLoc.addItem("&LN1008052067&LT153052067");
		comboLoc.addItem("&LN2119255067&LT223155067");
	}

	/**
	 * This method is called from within the constructor to initialize the form. WARNING: Do NOT
	 * modify this code. The content of this method is always regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		btnIniciar = new javax.swing.JButton();
		btnUserAlarm = new javax.swing.JButton();
		btnDeviceAlarm = new javax.swing.JButton();
		jScrollPane1 = new javax.swing.JScrollPane();
		textArea = new javax.swing.JTextArea();
		btnTechnicalAlarm = new javax.swing.JButton();
		txtURL = new javax.swing.JTextField();
		labelURL = new javax.swing.JLabel();
		txtTemp = new javax.swing.JTextField();
		txtBattery = new javax.swing.JTextField();
		labelTemp = new javax.swing.JLabel();
		labelBattery = new javax.swing.JLabel();
		txtnumber = new javax.swing.JTextField();
		labelnumber = new javax.swing.JLabel();
		comboLoc = new javax.swing.JComboBox();
		labelLoc = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("Samsung Galaxy Nexus");
		setResizable(false);

		btnIniciar.setText("Iniciar");
		btnIniciar.setPreferredSize(new java.awt.Dimension(120, 23));
		btnIniciar.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnIniciarActionPerformed(evt);
			}
		});

		btnUserAlarm.setText("User Alarm");
		btnUserAlarm.setPreferredSize(new java.awt.Dimension(120, 23));
		btnUserAlarm.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnUserAlarmActionPerformed(evt);
			}
		});

		btnDeviceAlarm.setText("Device Alarm");
		btnDeviceAlarm.setPreferredSize(new java.awt.Dimension(120, 23));
		btnDeviceAlarm.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnDeviceAlarmActionPerformed(evt);
			}
		});

		textArea.setColumns(20);
		textArea.setEditable(false);
		textArea.setRows(5);
		jScrollPane1.setViewportView(textArea);

		btnTechnicalAlarm.setText("Technical Alarm");
		btnTechnicalAlarm.setPreferredSize(new java.awt.Dimension(120, 23));
		btnTechnicalAlarm.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnTechnicalAlarmActionPerformed(evt);
			}
		});

		txtURL.setText("http://localhost:8080/tms-web/connector");
		txtURL.setPreferredSize(new java.awt.Dimension(250, 20));

		labelURL.setText("URL del servlet");

		txtTemp.setText("55");
		txtTemp.setPreferredSize(new java.awt.Dimension(250, 20));

		txtBattery.setText("5");
		txtBattery.setPreferredSize(new java.awt.Dimension(250, 20));

		labelTemp.setText("Temperatura (celsius)");

		labelBattery.setText("Bateria %");

		txtnumber.setText("666123123");
		txtnumber.setPreferredSize(new java.awt.Dimension(250, 20));

		labelnumber.setText("Número de móvil");

		labelLoc.setText("Localizacion");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(comboLoc,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														410, javax.swing.GroupLayout.PREFERRED_SIZE)
												.addGroup(
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING,
																				false)
																				.addComponent(
																						txtnumber,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						410,
																						Short.MAX_VALUE)
																				.addComponent(
																						txtURL,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						Short.MAX_VALUE)
																				.addComponent(
																						txtTemp,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						Short.MAX_VALUE)
																				.addComponent(
																						txtBattery,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						Short.MAX_VALUE))
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addGroup(
																						layout.createSequentialGroup()
																								.addGap(67,
																										67,
																										67)
																								.addGroup(
																										layout.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.LEADING)
																												.addComponent(
																														btnUserAlarm,
																														javax.swing.GroupLayout.PREFERRED_SIZE,
																														javax.swing.GroupLayout.DEFAULT_SIZE,
																														javax.swing.GroupLayout.PREFERRED_SIZE)
																												.addComponent(
																														btnIniciar,
																														javax.swing.GroupLayout.PREFERRED_SIZE,
																														javax.swing.GroupLayout.DEFAULT_SIZE,
																														javax.swing.GroupLayout.PREFERRED_SIZE)
																												.addComponent(
																														btnTechnicalAlarm,
																														javax.swing.GroupLayout.PREFERRED_SIZE,
																														javax.swing.GroupLayout.DEFAULT_SIZE,
																														javax.swing.GroupLayout.PREFERRED_SIZE)))
																				.addGroup(
																						javax.swing.GroupLayout.Alignment.TRAILING,
																						layout.createSequentialGroup()
																								.addPreferredGap(
																										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																								.addComponent(
																										btnDeviceAlarm,
																										javax.swing.GroupLayout.PREFERRED_SIZE,
																										javax.swing.GroupLayout.DEFAULT_SIZE,
																										javax.swing.GroupLayout.PREFERRED_SIZE))))
												.addComponent(labelURL)
												.addComponent(labelnumber)
												.addComponent(jScrollPane1,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														416, javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(labelTemp).addComponent(labelBattery)
												.addComponent(labelLoc))
								.addContainerGap(19, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addContainerGap()
						.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 200,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addGap(23, 23, 23)
						.addComponent(labelURL)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addGroup(
								layout.createParallelGroup(
										javax.swing.GroupLayout.Alignment.TRAILING)
										.addComponent(btnIniciar,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addComponent(txtURL,
												javax.swing.GroupLayout.PREFERRED_SIZE, 28,
												javax.swing.GroupLayout.PREFERRED_SIZE))
						.addGap(23, 23, 23)
						.addComponent(labelnumber)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addGroup(
								layout.createParallelGroup(
										javax.swing.GroupLayout.Alignment.LEADING)
										.addComponent(btnUserAlarm,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addComponent(txtnumber,
												javax.swing.GroupLayout.PREFERRED_SIZE, 26,
												javax.swing.GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(labelTemp)
						.addGap(5, 5, 5)
						.addGroup(
								layout.createParallelGroup(
										javax.swing.GroupLayout.Alignment.BASELINE)
										.addComponent(txtTemp,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addComponent(btnDeviceAlarm,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(labelBattery)
						.addGap(11, 11, 11)
						.addGroup(
								layout.createParallelGroup(
										javax.swing.GroupLayout.Alignment.LEADING, false)
										.addComponent(btnTechnicalAlarm,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addComponent(txtBattery,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE))
						.addGap(38, 38, 38)
						.addComponent(labelLoc)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(comboLoc, javax.swing.GroupLayout.PREFERRED_SIZE,
								javax.swing.GroupLayout.DEFAULT_SIZE,
								javax.swing.GroupLayout.PREFERRED_SIZE).addGap(48, 48, 48)));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void btnIniciarActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btnIniciarActionPerformed
		try {
			fachada.setBattery(txtBattery.getText());
			fachada.setSenderMobileNumber(txtnumber.getText());
			fachada.setTemperature(txtTemp.getText());
			fachada.setURLservlet(txtURL.getText());

			HttpResponse response = fachada.sendEmptyMessage();
			textArea.append("Requesting remote parameters...\n");

			if (200 == response.getStatusLine().getStatusCode()) {
				textArea.append("Parameters OK \n");
				String contents = fachada.readStreamAsString(response.getEntity().getContent());
				// parsear trama y obtener parametros individuales
				fachada.setParameters(contents);
				btnDeviceAlarm.setVisible(true);
				btnUserAlarm.setVisible(true);
				btnTechnicalAlarm.setVisible(true);
				btnIniciar.setVisible(false);
				// enviar ACK
				response = fachada.enviarACK();
				if (200 == response.getStatusLine().getStatusCode()) {
					;
				} else {
					System.out.println("ERROR!");
				}

			} else {
				textArea.append("ERROR receiving parameters \n");
			}
		} catch (URISyntaxException ex) {
			Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
		} catch (IOException ex) {
			Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
		}
	}// GEN-LAST:event_btnIniciarActionPerformed

	private void btnUserAlarmActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btnUserAlarmActionPerformed
		try {
			fachada.setBattery(txtBattery.getText());
			fachada.setSenderMobileNumber(txtnumber.getText());
			fachada.setTemperature(txtTemp.getText());
			fachada.setURLservlet(txtURL.getText());
			fachada.setLocation((String) comboLoc.getSelectedItem());

			textArea.append("Sending User Alarm... \n");
			HttpResponse response = fachada.enviarUserAlarm();
			if (200 == response.getStatusLine().getStatusCode()) {
				textArea.append("Sent OK! \n");
			} else {
				textArea.append("ERROR sending User Alarm \n");
			}
		} catch (URISyntaxException ex) {
			Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
		} catch (IOException ex) {
			Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
		}
	}// GEN-LAST:event_btnUserAlarmActionPerformed

	private void btnDeviceAlarmActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btnDeviceAlarmActionPerformed
		try {
			fachada.setBattery(txtBattery.getText());
			fachada.setSenderMobileNumber(txtnumber.getText());
			fachada.setTemperature(txtTemp.getText());
			fachada.setURLservlet(txtURL.getText());
			fachada.setLocation((String) comboLoc.getSelectedItem());

			textArea.append("Sending Device Alarm... \n");
			HttpResponse response = fachada.enviarDeviceAlarm();
			if (200 == response.getStatusLine().getStatusCode()) {
				textArea.append("Sent OK! \n");
			} else {
				textArea.append("ERROR sending Device Alarm \n");
			}
		} catch (URISyntaxException ex) {
			Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
		} catch (IOException ex) {
			Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
		}

	}// GEN-LAST:event_btnDeviceAlarmActionPerformed

	private void btnTechnicalAlarmActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btnTechnicalAlarmActionPerformed
		try {
			fachada.setBattery(txtBattery.getText());
			fachada.setSenderMobileNumber(txtnumber.getText());
			fachada.setTemperature(txtTemp.getText());
			fachada.setURLservlet(txtURL.getText());
			fachada.setLocation((String) comboLoc.getSelectedItem());

			textArea.append("Sending Technical Alarm... \n");
			HttpResponse response = fachada.enviarTechnicalAlarm();
			if (200 == response.getStatusLine().getStatusCode()) {
				textArea.append("Sent OK! \n");
			} else {
				textArea.append("ERROR sending Technical Alarm \n");
			}
		} catch (URISyntaxException ex) {
			Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
		} catch (IOException ex) {
			Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
		}

	}// GEN-LAST:event_btnTechnicalAlarmActionPerformed

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		/*
		 * Set the Nimbus look and feel
		 */
		// <editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the default look and
		 * feel. For details see
		 * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
					.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(Main.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(Main.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(Main.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(Main.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		}
		// </editor-fold>

		/*
		 * Create and display the form
		 */
		java.awt.EventQueue.invokeLater(new Runnable() {

			@Override
			public void run() {
				new Main().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton btnDeviceAlarm;
	private javax.swing.JButton btnIniciar;
	private javax.swing.JButton btnTechnicalAlarm;
	private javax.swing.JButton btnUserAlarm;
	private javax.swing.JComboBox comboLoc;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JLabel labelBattery;
	private javax.swing.JLabel labelLoc;
	private javax.swing.JLabel labelTemp;
	private javax.swing.JLabel labelURL;
	private javax.swing.JLabel labelnumber;
	private javax.swing.JTextArea textArea;
	private javax.swing.JTextField txtBattery;
	private javax.swing.JTextField txtTemp;
	private javax.swing.JTextField txtURL;
	private javax.swing.JTextField txtnumber;
	// End of variables declaration//GEN-END:variables
}
